package com.aruba.girlanime.delegate;

import android.animation.ValueAnimator;
import android.view.View;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;

/**
 * 房子动画
 * Created by aruba on 2018/4/17.
 */

public class HoushAnimeDelegate implements IAnimeDelegate {
    View iv_house_one;
    View iv_house_two;
    View iv_house_three;
    View iv_house_four;

    private boolean isInit;
    private int playDuration = 1000;

    @Override
    public void startAnime() {

        YoYo.with(Techniques.Swing)
                .duration((long) (playDuration * (Math.random() + 1)))
                .repeat(ValueAnimator.INFINITE)
                .playOn(iv_house_one);
        YoYo.with(Techniques.Swing)
                .duration((long) (playDuration * (Math.random() + 1)))
                .repeat(ValueAnimator.INFINITE)
                .repeat(ValueAnimator.INFINITE)
                .playOn(iv_house_two);
        YoYo.with(Techniques.Swing)
                .duration((long) (playDuration * (Math.random() + 1)))
                .repeat(ValueAnimator.INFINITE)
                .playOn(iv_house_three);
        YoYo.with(Techniques.Swing)
                .duration((long) (playDuration * (Math.random() + 1)))
                .repeat(ValueAnimator.INFINITE)
                .playOn(iv_house_four);
    }

    @Override
    public void init(View... args) {
        iv_house_one = args[0];
        iv_house_two = args[1];
        iv_house_three = args[2];
        iv_house_four = args[3];

        iv_house_four.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (!isInit) {
                    isInit = true;
                    startAnime();
                }
            }
        });
    }

    @Override
    public void stopAnime() {
        YoYo.with(Techniques.Swing).playOn(iv_house_one).stop();
        YoYo.with(Techniques.Swing).playOn(iv_house_two).stop();
        YoYo.with(Techniques.Swing).playOn(iv_house_three).stop();
        YoYo.with(Techniques.Swing).playOn(iv_house_four).stop();
    }

    @Override
    public void reset() {
        stopAnime();
    }
}
